﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using TinyRaytracer;

namespace Licenta1.Shapes
{
    [Serializable]
    class Line : Shape
    {
        Expression parameters;
        Point start, end;
        bool init = false;

        public Line(Context con, Expression param)
            : base(con)
        {
            parameters = param;
        }

        public Line(Context con, Point p1, Point p2)
            : base(con)
        {
            start = p1;
            end = p2;
            init = true;
        }

        private void CheckParameters()
        {
            object o1 = null, o2 = null;

            try
            {
                ParameterList p_list = (ParameterList)parameters;
                
                o1 = (Point)p_list.FirstValue;
                o2 = (Point)p_list.SecondValue;

                if (o1 != null)
                    start = (Point)o1;

                if (o2 != null)
                    end = (Point)o2;
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
                start = new Point(this.context, 0, 0);
                end = new Point(this.context, 0, 0);
            }
            init = false;
        }

        public override string Display()
        {
            if (!init)
                CheckParameters();
            init = true;
            return "{ " + start.Display() + ", " + end.Display() + " }";
        }

        public override object Value
        {
            get
            {
                if (!init)
                CheckParameters();
                return this;
            }
        }

        public override void Draw()
        {
            if (!init)
                CheckParameters();
            
            //context.mp.objectsList.Add(this.GetClone());
            context.mp.objectsList.Add(new TinyRaytracer.MainParsing.ObjectToDraw("line", (Shape)this.GetClone(), context.activeColor, context.activePen));
        }

        public override object GetClone()
        {
            return new Line(context, (Point)start.GetClone(), (Point)end.GetClone()); 
        }

        public Point START
        {
            get
            {
                if (!init)
                    CheckParameters();
                return start;
            }
            set { start = value; }
        }

        public Point END
        {
            get
            {
                if (!init)
                    CheckParameters();
                return end;
            }
            set { start = value; }
        }

        
    }
}